package com.sumainfor.pc.market.service.tenant.impl;

import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.market.dao.tenant.TenantInformationMapper;
import com.sumainfor.pc.market.service.tenant.TenantInformationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 合租人
 * <p>
 *
 * @author: zhoucong
 * @create: 2019-04-18 14:44
 **/
@Service
public class TenantInformationServiceImpl  implements TenantInformationService {
    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(TenantInformationServiceImpl.class);

    @Autowired
    TenantInformationMapper tenantInformationMapper;

    /**
     * 根据租客id查询合租人信息
     * @param params
     * @return
     */
    @Override
    public JsonResult getTenantInformation(Map<String,Object> params){
        JsonResult result = new JsonResult();
        List<Map<String,Object>> paraList = tenantInformationMapper.getTenantPact(params);
        if(paraList !=null&&paraList.size()>0){
        for (Map<String,Object> para:paraList) {
            List<Map<String,Object>> parList =tenantInformationMapper.getTenantInformation(para);
            para.put("parList",parList);
        }
        }
        return result.put(paraList);
    }


    /**
     * 添加合租人
     * @param params
     * @return
     */
    @Override
    public JsonResult insertTf(Map<String,Object> params){
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();

        try {
            Map<String,Object> para = new HashMap<>();
            para.put("PaId",params.get("PaId"));
            para.put("IdnoNumber",params.get("IdnoNumber"));
            System.out.println("para"+para);
            System.out.println("params"+params);
           Integer cout1 = tenantInformationMapper.selectTenantInformation(para);
            System.out.println("cout1"+cout1);
           if (cout1>0){
               result.putFailed("该合租人已存在...");
           }else {
               params.put("term", "1");//创建端  1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
               params.put("fnc",
                       this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
               ParamsCommon.commonInsertParms(params);
               System.out.println("params" + params);
               Integer cout2 = tenantInformationMapper.insertTenantInformation(params);
               //param.put("Coucode", ComUtils.getLocalTrmSeqNum("YHJ",a));
               if (cout2 != null && cout2 <= 0) {

                   result.putFailed("新增合租人失败");
               }
               result.putSuccess("新增合租人成功");
           }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
      return result;

    }

    /**
    * <p>
    * 删除合租人信息
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult deleteTenantInformation(Map<String,Object> params){
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("删除操作");
            System.out.println("params"+params);
            if(tenantInformationMapper.deleteTenantInformation(params)>0){
                result.putSuccess("删除合租人成功!");
            }else {
                result.putFailed("删除合租人失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }

      return result;
    }


}
